package com.luck.picture.lib;

import android.app.Application;

import com.danikula.videocache.HttpProxyCacheServer;

import java.io.File;

public class PictureApplication {

    private static Application mApplication;

    public static void init(Application application) {
        mApplication = application;
    }

    private static HttpProxyCacheServer proxy;
    private static HttpProxyCacheServer audioProxy;

    public static HttpProxyCacheServer getProxy() {
        return proxy == null ? (proxy = newProxy()) : proxy;
    }

    public static HttpProxyCacheServer getAudioProxy() {
        return audioProxy == null ? (audioProxy = newAudioProxy()) : audioProxy;
    }

    private static HttpProxyCacheServer newProxy() {
        File file = new File(mApplication.getExternalCacheDir() + File.separator + "videoCache");
        if (!file.exists()) {
            file.mkdirs();
        }
        return new HttpProxyCacheServer.Builder(mApplication)
                .cacheDirectory(file)//指定缓存路径
                .build();
    }

    private static HttpProxyCacheServer newAudioProxy() {
        File file = new File(mApplication.getExternalCacheDir() + File.separator + "audioCache");
        if (!file.exists()) {
            file.mkdirs();
        }
        return new HttpProxyCacheServer.Builder(mApplication)
                .cacheDirectory(file)//指定缓存路径
                .build();
    }

    public static String getAudioCachePath(String url) {
        final HttpProxyCacheServer proxy = PictureApplication.getAudioProxy();
        if (proxy == null) {
            return null;
        }
        return proxy.getProxyUrl(url);
    }
}
